package com.imau.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imau.bean.Teacher;
import com.imau.common.utils.SqlSessionFactoryUtil;
import com.imau.common.utils.StringLike;
import com.imau.mapper.TeacherMapper;
import com.imau.service.TeacherService;
import io.mybatis.mapper.example.Example;
import io.mybatis.service.AbstractService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class TeacherServiceImpl extends AbstractService<Teacher, String, TeacherMapper> implements TeacherService {


    @Override
    public Teacher save(Teacher teacher) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);

        //补全主键  创建人 创建时间


        Teacher save = super.save(teacher);
        session.close();

        return save;

    }

    @Override
    public int deleteById(String did) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);


        int i = super.deleteById(did);

        session.close();

        return i;
    }

    @Override
    public Teacher findById(String did) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);

        Teacher byId = super.findById(did);

        session.close();

        return byId;
    }

    @Override
    public List<Teacher> queryList(Teacher teacher) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);

        List<Teacher> list = super.findList(teacher);

        session.close();


        return list;
    }

    @Override
    public PageInfo<Teacher> queryListByPagte(Teacher teacher, int currentPage, int size) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);
        //1. 查询前构建分页对象

        PageHelper.startPage(currentPage, size);

        //2. 构建查询参数

        //2.1 创建 example 对象
        Example<Teacher> example = new Example<Teacher>();

        //2.2 创建 动态拼接Sql对象
        Example.Criteria<Teacher> criteria = example.createCriteriaSelective();

        if(teacher!=null)
        {
            criteria.andLike(Teacher::getName, StringLike.strCaonstLike(teacher.getName()));

            criteria.andEqualTo(Teacher::getPhone, teacher.getPhone());
            criteria.andEqualTo(Teacher::getDid, teacher.getDid());

        }
        //2.3 查询
        List<Teacher> Teachers = baseMapper.selectByExample(example);
        //2.4 封装成pageInfo 对象
        PageInfo<Teacher> pageInfo = new PageInfo<>(Teachers);

        session.close();
        return pageInfo;

    }

    @Override
    public int updateTeacher(Teacher teacher) {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(TeacherMapper.class);

        int i = baseMapper.updateByPrimaryKey(teacher);
        session.close();

        return i;
    }


}
